package swd.maintain.project.prggrp_right_maintain;

import com.pub.database.*;
import java.util.*;

public class Business {
   QueryBean gqbnDAO=null;
   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }
   String selectTableDataToFld(View view) throws Exception {
      String lstrSQL=null;
      Hashtable<String,Object> lhtTableData=null;
      try{
         lstrSQL="select p1,p2,p3,p4,p5,p6,p7,p8,prg_no,group_no from wkf_prg_right where group_no='" + view.getgroup_no() + "'" + " and prg_no='" + view.getprg_no() + "'";
         lhtTableData=gqbnDAO.readSingleRowData(lstrSQL);
         if(lhtTableData==null){
            return "查無資料";
         }
         view.setp1(String.valueOf((lhtTableData.get("P1").toString())));
         view.setp2(String.valueOf((lhtTableData.get("P2").toString())));
         view.setp3(String.valueOf((lhtTableData.get("P3").toString())));
         view.setp4(String.valueOf((lhtTableData.get("P4").toString())));
         view.setp5(String.valueOf((lhtTableData.get("P5").toString())));
         view.setp6(String.valueOf((lhtTableData.get("P6").toString())));
         view.setp7(String.valueOf((lhtTableData.get("P7").toString())));
         view.setp8(String.valueOf((lhtTableData.get("P8").toString())));
         view.setprg_no(String.valueOf((lhtTableData.get("PRG_NO").toString())));
         view.setgroup_no(String.valueOf((lhtTableData.get("GROUP_NO").toString())));

         return "";
      }
      catch(Exception e){
         throw new Exception(e.toString());
      }
      finally{
         lhtTableData.clear();
         lhtTableData=null;
      }
   }
   String exeUpdSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lvtrParam.add(view.getp1()==null ? null :String.valueOf(view.getp1()));
         lvtrParam.add(view.getp2()==null ? null :String.valueOf(view.getp2()));
         lvtrParam.add(view.getp3()==null ? null :String.valueOf(view.getp3()));
         lvtrParam.add(view.getp4()==null ? null :String.valueOf(view.getp4()));
         lvtrParam.add(view.getp5()==null ? null :String.valueOf(view.getp5()));
         lvtrParam.add(view.getp6()==null ? null :String.valueOf(view.getp6()));
         lvtrParam.add(view.getp7()==null ? null :String.valueOf(view.getp7()));
         lvtrParam.add(view.getp8()==null ? null :String.valueOf(view.getp8()));
         lvtrParam.add(view.getprg_no()==null ? null :String.valueOf(view.getprg_no()));
         lvtrParam.add(view.getgroup_no()==null ? null :String.valueOf(view.getgroup_no()));
         lstrSQL="update wkf_prg_right set p1=?,p2=?,p3=?,p4=?,p5=?,p6=?,p7=?,p8=?,prg_no=?,group_no=? where group_no='" + view.getgroup_no() + "'" + " and prg_no='" + view.getprg_no() + "'";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){
            return "wkf_prg_right";
         }
         return "";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e.toString());
      }
      finally{
         lvtrParam.clear();
      }
   }
   String exeInsSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lvtrParam.add((view.getprg_no()==null ? null : view.getprg_no()));
         lvtrParam.add((view.getgroup_no()==null ? null : view.getgroup_no()));
         lvtrParam.add((view.getp1()==null ? null : view.getp1()));
         lvtrParam.add((view.getp2()==null ? null : view.getp2()));
         lvtrParam.add((view.getp3()==null ? null : view.getp3()));
         lvtrParam.add((view.getp4()==null ? null : view.getp4()));
         lvtrParam.add((view.getp5()==null ? null : view.getp5()));
         lvtrParam.add((view.getp6()==null ? null : view.getp6()));
         lvtrParam.add((view.getp7()==null ? null : view.getp7()));
         lvtrParam.add((view.getp8()==null ? null : view.getp8()));
         lstrSQL="insert into wkf_prg_right(prg_no,group_no,p1,p2,p3,p4,p5,p6,p7,p8) values (?,?,?,?,?,?,?,?,?,?)";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){;
            return "wkf_prg_right";
         }
         lvtrParam.clear();

         saveDataToWkf_User(view.getgroup_no());

         return "";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e);
      }
      finally{
         lvtrParam.clear();
      }
   }

   void exeDelSQL(View view,int pintSn){
      String lstrSQL=new String();

      try{
         lstrSQL="delete from wkf_prg_right where prg_no='"+view.getprg_no_g()[pintSn].trim()+"' and group_no='"+view.getgroup_no_g()[pintSn]+"' ";
         gqbnDAO.executeSQL(lstrSQL);
      } catch(Exception e){
         e.printStackTrace();
      }
   }

   void saveDataToWkf_User(String strGroupNo){
       // 自動將Group加入至wkf_user
       String strExitYn="0";
       String lstrSQL=new String();
       Vector<Object> lvtrParam = new Vector<Object>();
       strExitYn = gqbnDAO.readSingleValueData("select count(*) from wkf_user" +
                                               " where data_kind='G'" +
                                               "   and user_no='" + strGroupNo + "'");
       if (strExitYn.equals("0")){
          try{
             lvtrParam.add(strGroupNo);
             lvtrParam.add(strGroupNo);
             lvtrParam.add("1");
             lvtrParam.add("G");
             lstrSQL="insert into wkf_user " +
                     "  (user_no,user_name,password,data_kind)" +
                     " values (?,?,?,?)";
             gqbnDAO.executeSQL(lstrSQL,lvtrParam);
             lvtrParam.clear();
          } catch(Exception e){
             e.printStackTrace();
          } finally{
             lvtrParam.clear();
          }
       }
   }



   void setDataViewForm(DataViewForm DataViewForm,View view,String pstrLoginUser) throws Exception{

       //StringBuffer stbCondition = new StringBuffer();
       StringBuffer stbPageActionURI = new StringBuffer("grpprg_right_maintain.jsp?rowsn=");

       DataViewForm.setFieldsList(" '',group_no,prg_no");
       DataViewForm.setTablesList(" wkf_prg_right ");
       DataViewForm.setSQLCondition("prg_no = '" + view.getprg_no() + "' ");
       DataViewForm.setOrderByFields("group_no");
       DataViewForm.setOrderBySeq("asc");
       DataViewForm.setGroupBy(null);
       DataViewForm.setHaving(null);

       String strColRelation[][] = new String[4][];

       strColRelation[0] = new String[5];
       strColRelation[1] = new String[8];
       strColRelation[2] = new String[5];
       strColRelation[3] = new String[5];

       strColRelation[0][0] = "''";
       strColRelation[0][1] = "INPUT";
       strColRelation[0][2] = "WIDTH='10%' align='center'";
       strColRelation[0][3] = "<INPUT class='button b_normal' TYPE=\"BUTTON\" onclick=\"javascript:saveRowSn('%sn%');document.all.item('prgAction').value='delete';document.forms[0].submit();\" NAME=\"del\" VALUE=";
       strColRelation[0][4] = "'刪除'";

       strColRelation[1][0]="'群組名稱'";
       strColRelation[1][1]="LINK";
       strColRelation[1][2]=" nowrap WIDTH='90%' ";
       strColRelation[1][3]="<a target='_blank' href=";
       strColRelation[1][4]="1";
       strColRelation[1][5]="'user_group_maintain.jsp'";
       strColRelation[1][6]="group_no=";
       strColRelation[1][7]="1";


       strColRelation[2][0]="HIDDEN";
       strColRelation[2][1]="INPUT";
       strColRelation[2][2]="WIDTH='0%'";
       strColRelation[2][3]="<INPUT TYPE='HIDDEN' size='8' name='group_no_g' VALUE=";
       strColRelation[2][4]="1";

       strColRelation[3][0]="HIDDEN";
       strColRelation[3][1]="INPUT";
       strColRelation[3][2]="WIDTH='0%'";
       strColRelation[3][3]="<INPUT TYPE='HIDDEN' size='8' name='prg_no_g' VALUE=";
       strColRelation[3][4]="2";


       DataViewForm.setColRelation(strColRelation);//將欄位相關設定殖入ActionForm
       DataViewForm.setJumpPageButtonShow(false);   //是否要秀出上下頁按鈕
       DataViewForm.setPageBtnOnTop(false);         //將button and rows info 秀在頂端
       DataViewForm.setPageBtnOnButtom(true);      //將button and rows info 秀在底端
       DataViewForm.setFilterShow(false);           //是否要秀出過濾選擇欄位
       DataViewForm.setJumpPageShow(false);         //是否要秀出跳頁下拉式選單
       DataViewForm.setRowsInfoShow(false);         //是否要秀出筆數資訊
       DataViewForm.setRowPageShow(false);          //是否要秀出每頁筆數控制欄
       DataViewForm.setExcelShow(false);           //是否要秀出轉Excel button
       DataViewForm.setOrderEnable(false);          //是否要提供排序點選功能
       DataViewForm.setShowHeaderWhenNoData(false);//當ResultSet無資料是否要秀Header

       DataViewForm.setTableHeaderHtml("class='td_head_grid' align='center'  width='680px'");
             //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
       DataViewForm.setPageActionURI(stbPageActionURI.toString());
   }


   String checkNotNullFld(View view) throws Exception{
      StringBuffer lstbNullFldList=new StringBuffer();
      return (lstbNullFldList.toString().equals("") ? "" : lstbNullFldList.delete(lstbNullFldList.length()-1,lstbNullFldList.length()).toString());
   }
   void setInitValueForAdd(View view,String pstrUserID) throws Exception{
      view.setp1("Y");
      view.setp2("Y");
      view.setp3("N");
      view.setp4("Y");
      view.setp5("Y");
      view.setp6("Y");
      view.setp7("N");
      view.setp8("N");
   }
   void setReadonlyForAdd(View view) throws Exception{
       view.setprg_no_readonly(true);
       view.setgroup_no_readonly(false);
   }

}
